--
-- Created by IntelliJ IDEA.
-- Author: yao.chen
-- Date: 2018/12/12
-- GiftRender.lua
-- 礼包直购Render
--

local GoddessWishBaseRender = require("app.boss.views.renders.GoddessWishBaseRender")
local math_min = math.min
local string_split = string.split
local string_format = string.format

local ClassRef = app.mvc.createPanelClass(...)

function ClassRef.create(node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.GiftUnitNode))
    sprite:init()
    return sprite
end

function ClassRef:init()
    self.super.init(self, data)

    self._title = self._ui.tlt
    self._text = self._ui.text
    self._titleBG = self._ui.bg_tlt

    self._buyButtonName = self._ui.name
    self._ui.Button_buy:addTouchEventListener(function(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            if self._type ~= Enums.GiftType.TaskGift then
                -- 点击后设置按钮1s不可点，防止连点操作
                display.setBtnTouchEnable(self._ui.Button_buy, false)
                self._btnAction = self._ui.Button_buy:performWithDelay(function()
                    self._btnAction = nil
                    display.setBtnTouchEnable(self._ui.Button_buy, true)
                end, 0.5)
            end
            executeFunc(self._onOperate, self, self._data)
        end
    end)

    local renders = {0, 0, 0}
    for i = 1, #renders do
        local icon_panel = self._ui["icon_" .. i]
        local render = GoddessWishBaseRender.create(icon_panel:getChildByName("icon"))
        render:setTipEnable(true)
        render:setShowOrign(true)
        renders[i] = render
    end
    self._giftIconRenders = renders

    self._type = Enums.GiftType.RMBGift
    self._index = 1
end

function ClassRef:getItemByConf(array)
    local items = {}
    for _, v in ipairs(array) do
        local dropItem = UD:parseRewardToBagItem(UD:parseClassText(v, UD:getClass()))
        table.insert(items, dropItem)
    end
    return items
end

--Enums.GiftType
function ClassRef:setGiftType(type)
    self._type = type
end
function ClassRef:getGiftType()
    return self._type
end

function ClassRef:setIndex(index)
    self._index = index
end

function ClassRef:_clearBtnAction()
    if self._btnAction then
        self._ui.Button_buy:stopAction(self._btnAction)
        self._btnAction = nil
    end
end

function ClassRef:setCanBuy(canBuy)
    self:_clearBtnAction()
    display.setBtnTouchEnable(self._ui.Button_buy, canBuy)
end

function ClassRef:refresh()
    if not self._data then return end
    self:_clearBtnAction()

    local data = self._data
    if self._type == Enums.GiftType.RMBGift then
        self:refreshRender_RMBGift(data)
    elseif self._type == Enums.GiftType.DailyGift then
        self:refreshRender_DailyGift(data)
    elseif self._type == Enums.GiftType.TaskGift then
        self:refreshRender_TaskGift(data)
    end
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

----------------------------------------------------------
--message RechargeGoodsInfo {
--    int32 goodsID = 1;
--string product_id = 2;
--int32 price = 3;
--int32 showPrice = 4;
--int32 hotGoods = 5;
--string demond = 6;
--string extraGift = 7;
--string firstExtraGift = 8;
--int32 firstExtraGiftCD = 9;
--int32 num = 10;
--int32 numType = 11;
--int32 shopType = 12;
--string icon = 13;
--}
--tasks = {
--    int32 needProgress = 1;	//需要的数量
--    int32 nowProgress = 2;	//现在的数量
--    bool isCanget = 3;	//是否能领取
--}
function ClassRef:refreshRender_DailyGift(data)
    local conf = GD:getRechargeForeignConfById(data.ID)
    if not conf then return end
    local items = UD:parseDropIDToBagItem(tonumber(conf.drop))

    display.setBtnTouchEnable(self._ui.Button_buy, data.num > 0)
    if data.num > 0 then
        self._buyButtonName:setString(string_format(L("dailyPacketBuyButton"), data.price))
    else
        self._buyButtonName:setString(L("tips_3000411"))
    end
    local str = "everydayPackettips" .. self._index
    self._text:setString(string_format(L(str), data.nowProgress))
    str = "everydayPacketTitle" .. self._index
    self._title:setString(L(str))

    if not items then return end

    local renders = self._giftIconRenders
    local count = math_min(#renders, #items)    -- 防止越界
    for i = 1, count do
        local item = items[i]
        if item.quality then
            local icon_panel = self._ui["icon_" .. i]
            icon_panel:findChild("bg"):setColor(Enums.QualityColor[item.quality])
        end
        renders[i]:setData(item)
    end
end

-- 完成任务的礼包
function ClassRef:refreshRender_TaskGift(data)
    local par = GD:getSystemPar("everydayPacketReward")
    local items = UD:parseDropIDToBagItem(tonumber(par))
    self._text:setString(string_format(L("everydayPackettips1"), data.nowProgress))
    self._title:setString(L("everydayPacketTitle1"))

    display.setBtnTouchEnable(self._ui.Button_buy, true)
    if data.nowProgress >= data.needProgress then
        if data.isCanget then
            self._buyButtonName:setString(L("dungeon_firstKill_string7"))
        else
            self._buyButtonName:setString(L("tips_2000236"))
            display.setBtnTouchEnable(self._ui.Button_buy, false)
        end
    else
        self._buyButtonName:setString(L("task_daily_desc1"))
    end

    local renders = self._giftIconRenders
    local count = math_min(#renders, #items)    -- 防止越界
    for i = 1, count do
        local item = items[i]
        if item.quality then
            local icon_panel = self._ui["icon_" .. i]
            icon_panel:findChild("bg"):setColor(Enums.QualityColor[item.quality])
        end
        renders[i]:setData(item)
    end
end

----------------------------------------------------------
-- data = {
--     giftID = 商品id,
--     isCanBuy = true/false,
--     conf = 配置,
-- }
function ClassRef:refreshRender_RMBGift(data)
    local conf = data.conf
    local array = string_split(conf.rewardshow, ";")
    local items = self:getItemByConf(array)

    local renders = self._giftIconRenders
    local count = math_min(#renders, #items)    -- 防止越界
    for i = 1, count do
        local item = items[i]
        if item.quality then
            local icon_panel = self._ui["icon_" .. i]
            icon_panel:findChild("bg"):setColor(Enums.QualityColor[item.quality])
        end
        renders[i]:setData(item)
    end

    self._text:setString(L(string.gsub(conf.desc, "<br />", "\n")..""))
    self._title:setString(L(conf.name))
    local path = Res.GiftPackageBG[tonumber(conf.color)]
    if path then
        self._titleBG:loadTexture(path)
    end

    display.setBtnTouchEnable(self._ui.Button_buy, data.isCanBuy)
end


return ClassRef

